<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="工厂">
          <el-select
            v-model="searchParam.plantId"
            placeholder="请选择工厂"
            @change="PlanChange"
            clearable
          >
            <el-option
              v-for="item in plants"
              :key="item.plantId"
              :label="item.plantName"
              :value="item.plantId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="到货日期">
          <el-input
            v-model="searchParam.arriveDate"
            placeholder="到货日期"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="交货地点">
          <el-select
            v-model="searchParam.storageLocationId"
            placeholder="请选择交货地点"
            clearable
          >
            <el-option
              v-for="item in storageLocations"
              :key="item.id"
              :label="item.storageLocationName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="时间窗">
          <el-time-select
            v-model="searchParam.time"
            style="width: 240px"
            start="08:00"
            step="00:30"
            end="18:30"
          />
        </el-form-item>
        <el-form-item label="供应商id" style="display: none">
          <el-input
            v-model="searchParam.partnerId"
            placeholder="供应商id"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openTransferDialog()"
          ><i-ep-plus />创建</el-button
        >
      </template>

      <el-table
        :height="498"
        highlight-current-row
        :header-cell-style="{ backgroundColor: '#F0F8FF' }"
        border
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          prop="id"
          label="物料编号"
          align="center"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          prop="name"
          label="物料名称"
          align="center"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          prop="name"
          label="包装条码"
          align="center"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          prop="name"
          label="生产批次"
          align="center"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          prop="name"
          label="包装容量"
          align="center"
          min-width="100"
          show-overflow-tooltip
        />
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <delivery-edit
      :visible="transferDialog.visible"
      :title="transferDialog.title"
      :data="transferDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "SRMSRMDELIVERYNOTE",
  inheritAttrs: false,
});

const transferDialog = reactive<DialogOption>({
  visible: false,
  title: "",
  data: {},
});

function openTransferDialog() {
  transferDialog.visible = true;
  transferDialog.title = "新增发货单";
}
const value = ref("");
// 请求参数
const requestApi = "";

// 是否需要分页
const pagination = true;

const initParam = {
  sort: {
    column: "last_update_time",
    orderBy: "desc",
  },
};

// 初始化搜索参数数据
const searchParamInit = {
  partnerId: "",
  time: "",
  plantId: "",
  arriveDate: "",
  storageLocationId: "",
};

const deleteParam = {
  api: "/srm/core/mdmpartner/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行
// 工厂
let plants: any = ref([]);
// 要货地点
let storageLocations: any = ref([]);
// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  // getTableList();
  //todo 通过权限获取登陆人供应商id getLoginInfo(); 供应商签订协议的公司的工厂
  // getPartnerPlants("1744249727643115521");
});

function changeVisible(value: boolean) {
  transferDialog.visible = value;
  if (!value) {
    transferDialog.data = {};
  }
}

function trim(val) {
  if (val) {
    return val;
  } else {
    return "";
  }
}
/** 预留权限请求 */
async function getLoginInfo() {
  request({
    url: `获取权限`,
    method: "post",
  }).then((data) => {
    // todo  searchParam.value.partnerId = data.data 查询条件添加供应商id
    //根据供应商id查询该供应商与工厂的计划
    getPartnerPlants(data.data);
  });
}
//获取供应商合作工厂
async function getPartnerPlants(id: string) {
  // todo 一下两列临时存放第一个编辑后重置页面搜索内容(不重置登陆人id),第二个供应商id赋值 每次查询带上当条件
  searchParamInit.partnerId = id;
  searchParam.value.partnerId = id;

  request({
    url: `/srm/partner/deliverynote/plant/${id}`,
    method: "get",
  }).then((data) => {
    plants.value = data.data;
  });
}
//选择工厂时根据工厂id查询存储地
async function PlanChange() {
  request({
    url: `/srm/core/storagelocation/getList`,
    method: "post",
    data: { plantId: searchParamInit.plantId },
  }).then((data) => {
    storageLocations.value = data.data;
  });
}
</script>
